package com.yc.io;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

public class TestInputStream {
	
	/**
	 * 测试new一个InputStream,直接使用文件路径
	 * @throws FileNotFoundException 
	 */
	public void test1() throws FileNotFoundException {
		String path = "F:\\worksource\\JKCT\\HB\\temp\\黑椒大排.jpg";
		File file = new File(path);
		InputStream is = new FileInputStream(file);
//		InputStream is = new FileInputStream(name)
		
	}
	
	/**
	 * 测试把inputstream转成ByteArrayInputStream，读取完ByteArrayInputStream后（不重置标记），inputstream是否还能继续读取数据
	 * 结论：有一点和预想是一样：InputStream————>ByteArrayInputStream,然后取干净ByteArrayInputStream，InputStream就空了
	 * 意料之外的发现：InputStream————>ByteArrayInputStream 这个转换完成后，InputStream就已经空了。和读不读ByteArrayInputStream没有关系
	 * 解释：实际转换过程：InputStream——byte[]——>ByteArrayInputStream，也就是说转换的过程，就把InputStream“抽干了”
	 * @throws IOException 
	 */
	public void testByteArrayInputStream() throws IOException {
		String path = "F:\\worksource\\JKCT\\HB\\temp\\黑椒大排.jpg";
		File file = new File(path);
		int size = Long.valueOf(file.length()).intValue();
		System.out.println("size="+ size);
		InputStream is = new FileInputStream(file);
		ByteArrayInputStream in = new ByteArrayInputStream(IOUtils.toByteArray(is));
		in.mark(size);
		
		byte[] buf0 = new byte[1024];
		int len0 = is.read(buf0);
		System.out.println("len0="+ len0);
		
		byte[] buf1 = new byte[size];
		int len1 = in.read(buf1);
		System.out.println("len1="+ len1);
		
		byte[] buf2 = new byte[1024];
		int len2 = is.read(buf2);
		System.out.println("len2="+ len2);
		
		
		in.reset();
		byte[] buf3 = new byte[1024];
		int len3 = is.read(buf3);
		System.out.println("len3="+ len3);
		
		
	}

	public static void main(String[] args) throws Exception {
		TestInputStream ts = new TestInputStream();
		ts.testByteArrayInputStream();

	}

}
